﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Configuration;
using Repository;
using UI.Areas.Admin.Models;
using UI.Helper;

namespace UI.Areas.Admin.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Admin/User/

        public ActionResult Index()
        {
            var repository = RepositoryManager.CreateUserRepository();

            var userId = 0;

            if (Request.Cookies[Constants.USER_ID_SESSION] != null)
                userId = Convert.ToInt32(Request.Cookies[Constants.USER_ID_SESSION].Value);

            var model = new UserModel() {Id = userId};

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(UserModel model)
        {
            var repository = RepositoryManager.CreateUserRepository();

            if (model != null)
            {
                var objUser = repository.GetById(model.Id);

                if (objUser != null)
                {
                    if (objUser.Password != model.OldPassword)
                        ViewData[Constants.MESSAGE] = "Mật khẩu cũ không đúng";
                    else
                    {
                        objUser.Password = model.NewPassword;

                        repository.Update(objUser);

                        ViewData[Constants.MESSAGE] = "Đã thay đổi mật khẩu thành công";
                    }
                }    
            }

            return View(model);
        }
    }
}
